<?php
namespace App\Services;

use Illuminate\Support\Facades\DB;
/**
 *
 * 获取用户所在地区的邮费
 *
 * @author shengl
 *
 */
class LogisticsService extends BaseService {
        /**
         * 获取用户所在地区的默认邮费
         */
        public function getPostage($goods_id,$region_id,$table = ''){

            if($table !=''){
                $logistics = DB::table('landowners')
                    ->leftJoin('logisticst_tmpl', 'logisticst_tmpl.logistics_id', '=', 'landowners.logistics_id')
                    ->where('landowners_id',$goods_id)->first();
            }else{
                $logistics = DB::table('goods')
                    ->leftJoin('logisticst_tmpl', 'logisticst_tmpl.logistics_id', '=', 'goods.logistics_id')
                    ->where('goods_id',$goods_id)->first();
            }
            if(!$logistics){
                $postage=0;
                return $postage;
            }
            if($logistics->free_shipping == 0){
                $postage = 0;
                return $postage;
            }
            $shipping_method = json_decode($logistics->shipping_method,true);
            if(count($shipping_method)>0){
                foreach($shipping_method as $k=>$v){
                    $region_arr = explode(',',$v['toregions']);
                    if(in_array($region_id,$region_arr)){
                        $postage="首重".$v['first_money']."(元) 续重".$v['continued_money']."(元)";
                        return $postage;
                    }
                }
                $postage="首重".$shipping_method[0]['first_money']."(元) 续重".$shipping_method[0]['continued_money']."(元)";
                return $postage;
            }
            $postage = 0;
            return $postage;
        }

    /**
     * 获取用户所在地区的实际邮费
     */
    public function getLogisticsMoney($goods_id,$region_id,$quantity,$table=''){

        $postage =[
            'postage'=>'',//商品邮费
            'weight'=>'',//商品重量
            'first_heavy'=>'',//商品首重
            'first_money'=>'',//商品首费
            'continued_money'=>'',//商品续费
            'continued_heavy'=>'',//商品续重
        ];

        //取出所需数据
        if($table ==''){
            $logistics = DB::table('goods')
                ->leftJoin('logisticst_tmpl', 'logisticst_tmpl.logistics_id', '=', 'goods.logistics_id')
                ->where('goods_id',$goods_id)->first();
        }else{
            $logistics = DB::table('landowners')
                ->leftJoin('logisticst_tmpl', 'logisticst_tmpl.logistics_id', '=', 'landowners.logistics_id')
                ->where('landowners_id',$goods_id)->first();
        }

        //如果商品未绑定模板
        if(!$logistics){
            $postage['postage']=0;
            return $postage;
        }

        //如果是包邮模板
        if($logistics->free_shipping == 0){
            $postage['postage']=0;
            return $postage;
        }

        //计算邮费
        $weight = $logistics->weight;
        $shipping_method = json_decode($logistics->shipping_method,true);
        if(count($shipping_method)>0){
            foreach($shipping_method as $k=>$v){
                $region_arr = explode(',',$v['toregions']);
                //如果配送地区存在于模板中
                if(in_array($region_id,$region_arr)){
                    $continued_heavy = $weight*$quantity - $v['first_heavy'];

                    if($continued_heavy<=0){ //如果商品重量小于首重 取出首费
                        $postage['postage'] =  $v['first_money'];
                        $postage['weight'] =  $weight;
                        $postage['first_heavy'] = $v['first_heavy'];
                        $postage['first_money'] = $v['first_money'];
                        $postage['continued_money'] = $v['continued_money'];
                        $postage['continued_heavy'] = $v['continued_heavy'];
                        return $postage;
                    }else{//商品重量大于首重 计算续重价格
                        $postage['postage'] =  $v['first_money'] + ceil($continued_heavy/$this->_getNum($v['continued_heavy']))*$this->_getNum($v['continued_money']);
                        $postage['weight'] =  $weight;
                        $postage['first_heavy'] = $v['first_heavy'];
                        $postage['first_money'] = $v['first_money'];
                        $postage['continued_money'] = $v['continued_money'];
                        $postage['continued_heavy'] = $v['continued_heavy'];
                        return $postage;
                    }

                }
            }
            //如果配送地区不存在选定的地区取默认运费
            $continued_heavy = $weight*$quantity - $shipping_method[0]['first_heavy'];
            if($continued_heavy<=0){   //如果商品重量小于首重 取出首费
                $postage['postage'] =  $shipping_method[0]['first_money'];
                $postage['weight'] =  $weight;
                $postage['first_heavy'] = $shipping_method[0]['first_heavy'];
                $postage['first_money'] =$shipping_method[0]['first_money'];
                $postage['continued_money'] = $shipping_method[0]['continued_money'];
                $postage['continued_heavy'] = $shipping_method[0]['continued_heavy'];
                return $postage;
            }else{ //商品重量大于首重 计算续重价格
                $postage['postage'] =   $shipping_method[0]['first_money'] + ceil($continued_heavy/$this->_getNum($shipping_method[0]['continued_heavy']))*$this->_getNum($shipping_method[0]['continued_money']);
                $postage['weight'] =  $weight;
                $postage['first_heavy'] = $shipping_method[0]['first_heavy'];
                $postage['first_money'] =$shipping_method[0]['first_money'];
                $postage['continued_money'] = $shipping_method[0]['continued_money'];
                $postage['continued_heavy'] = $shipping_method[0]['continued_heavy'];
                return $postage;
            }
        }
        $postage['postage'] = 0;
        return $postage;
    }

//把值修改为有效数字 避免运算出错
    public function _getNum($num){

       if($num <=0 ){
          $num = 1;
       }
        return $num;
    }


}